import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Util {
    public static Image getImage(String path){
        return new ImageIcon(Util.class.getResource(path)).getImage();
    }
    public static ImageIcon getImageIcon(String path){
        return new ImageIcon(Util.class.getResource(path));
    }


    /**
     * 创建连接
     * @return
     */
    public static Connection getConnection() {
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        // 2. 创建连接
        Connection con = null;
        try {
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/j189?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "231725");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println("con:" + con);
        return con;
    }


    /**
     * 更新数据
     *
     * @throws SQLException
     */
    public static void insert(String sql) {
        //1.创建连接
        Connection con = getConnection();
        Statement statement = null;
        try {
            //2.获取执行SQL语句对象
            statement = con.createStatement();
            //3.执行sql语句
            int row = statement.executeUpdate(sql);
//            System.out.println("影响的行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //4.关闭连接
            close(con, statement);
        }
    }

    /**
     * 关闭连接
     *
     * @param con
     * @param statement
     */
    public static void close(Connection con, Statement statement) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 关闭连接 重载
     *
     * @param con
     * @param statement
     * @param rs
     */
    public static void close(Connection con, Statement statement, ResultSet rs) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

    /**
     * 查询
     */
    public static ArrayList query() {
        Connection con = getConnection();
        Statement statement = null;
        ResultSet rs = null;
        ArrayList<User> userlist=new ArrayList();
        try {
            //2.获取执行SQL语句的对象
            statement = con.createStatement();
            String sql = "select u_name,u_psw,u_money from t_user";
            //3.执行SQL并返回结果集
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                User user =new User();
                user.setUsername(rs.getString(1));
                user.setPassword(rs.getString(2));
                user.setMoney(rs.getDouble(3));
                userlist.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //4.关闭
            close(con, statement, rs);
        }
        return userlist;
    }

}